package com.yaorange.web.controller.certificate.wxapi;

import com.yaorange.certificate.domain.NjtUserPerfect;
import com.yaorange.certificate.domain.NjtUserRegister;
import com.yaorange.certificate.service.INjtUserRegisterService;
import com.yaorange.certificate.vo.NjtLoginVo;
import com.yaorange.certificate.vo.NjtRegisterVo;
import com.yaorange.common.core.domain.AjaxResult;
import com.yaorange.common.core.domain.model.LoginUser;
import com.yaorange.common.utils.SecurityUtils;
import com.yaorange.common.utils.SmsUtils;
import com.yaorange.common.utils.StringUtils;
import com.yaorange.common.utils.ValidateCodeUtils;
import com.yaorange.framework.web.service.SysLoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

/**
 * @author coach tam
 * @email 327395128@qq.com
 * @values 坚持灵活 灵活坚持
 * @since 2024/7/15
 */
@Slf4j
@RestController
@RequestMapping("/api/user")
public class NjtUserAPI{
    // 定义手机号的正则表达式
    private static final String PHONE_NUMBER_REGEX = "^1[3-9]\\d{9}$";

    public static final String NJT_CODE_REPEAT = "njt:code:repeat:";
    public static final String NJT_CODE_VALIDATE = "njt:code:validate:";

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private SmsUtils smsUtils;

    @Autowired
    private INjtUserRegisterService userRegisterService;

    @Autowired
    private SysLoginService sysLoginService;



    @GetMapping("/sendValidateCode")
    public AjaxResult sendValidateCode(@RequestParam("mobilePhone") String mobilePhone){
        //1.校验手机号码格式
        if(!Pattern.compile(PHONE_NUMBER_REGEX).matcher(mobilePhone).matches()){
            return AjaxResult.error("手机号码格式不正确");
        }
        //2.校验是否可以重发
    /*    if(stringRedisTemplate.hasKey(NJT_CODE_REPEAT +mobilePhone)){
            return AjaxResult.error("一个手机号1分钟以内只能发送一次");
        }*/
        //3.生成6位验证码
        String code = ValidateCodeUtils.generateValidateCode(4);
        //4.保存验证码到redis
        stringRedisTemplate.opsForValue().set(NJT_CODE_REPEAT + mobilePhone, code, 1, TimeUnit.MINUTES);
        stringRedisTemplate.opsForValue().set(NJT_CODE_VALIDATE + mobilePhone, code, 60*5,TimeUnit.SECONDS);

        //5.发送验证码
        log.info("正在发送验证码[{}]到手机号[{}]", code, mobilePhone);

//        smsUtils.sendSms(mobilePhone, code);

        return AjaxResult.success();
    }

    @GetMapping("/checkMobilePhone")
    public AjaxResult checkMobilePhone(@RequestParam("mobilePhone") String mobilePhone){
        NjtUserRegister user = userRegisterService.findByMobilePhone(mobilePhone);
        if(user == null){
            return AjaxResult.success();
        }else {
            return AjaxResult.error("手机号已注册");
        }
    }

    @PostMapping("/register")
    public AjaxResult register(@RequestBody NjtRegisterVo njtRegisterVo) {
        //1.校验验证码
        String checkCode = njtRegisterVo.getCheckCode();
        String redisCode = stringRedisTemplate.opsForValue().get(NjtUserAPI.NJT_CODE_VALIDATE + njtRegisterVo.getMobilePhone());
        if(StringUtils.isEmpty(redisCode) || !checkCode.equals(redisCode)){
            return AjaxResult.error("验证码错误");
        }
        //2.校验手机号码格式
        userRegisterService.register(njtRegisterVo);
        return AjaxResult.success();
    }

    @PostMapping("/login")
    public AjaxResult login(@RequestBody NjtLoginVo njtLoginVo) {
        AjaxResult success = AjaxResult.success();
        String token = sysLoginService.appLogin(njtLoginVo.getUsername(), njtLoginVo.getPassword());
        success.put("token",token);
        return success;
    }

    @GetMapping("/queryLoginUser")
    public AjaxResult queryLoginUser() {
          return userRegisterService.getLoginUser();
    }

    @PostMapping("/perfect")
    public AjaxResult perfect(@RequestBody NjtUserPerfect perfect) {
        userRegisterService.perfect(perfect);
        return AjaxResult.success();
    }
}
